package com.hz.friends.activity;

import java.util.List;

import com.androidquery.AQuery;
import com.androidquery.callback.AjaxStatus;
import com.androidquery.callback.BitmapAjaxCallback;
import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVUser;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.hz.friends.application.CustomApplication;
import com.hz.friends.chat.GroupChatActivity;
import com.hz.friends.serverrequest.ALCCPEngine;
import com.hz.friends.serverrequest.ALEngineConfig.CallBack;
import com.hz.friends.serverrequest.ALUserEngine;
import com.hz.friends.utils.BitmapUtils;
import com.hz.friends.utils.DensityUtil;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class GroupMembersActivity extends BaseActivity {
	private PullToRefreshListView mlistview;
	private GroupAdpter adpter;
	private AVObject group ;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_groupmembers);
		init();
		initViews();
		initEvents();
		uploadGroupMembers();
	}

	@Override
	protected void init() {
		group = CustomApplication.getInstance().currgroup;
	}

	@Override
	protected void initViews() {
		ImageButton imbtn_bar_left = (ImageButton) findViewById(R.id.imbtn_bar_left);
		ImageButton imbtn_bar_right = (ImageButton) findViewById(R.id.imbtn_bar_right);
		TextView imbtn_bar_title = (TextView) findViewById(R.id.imbtn_bar_title);
		imbtn_bar_right.setVisibility(View.INVISIBLE);
		imbtn_bar_left.setImageResource(R.drawable.ic_leftback_arrow);
		imbtn_bar_title.setText("group members");
		imbtn_bar_left.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});

		mlistview = (PullToRefreshListView) findViewById(R.id.mlistview);
		adpter = new GroupAdpter(this);
		mlistview.setAdapter(adpter);
	}

	@Override
	protected void setViews() {
		// TODO Auto-generated method stub

	}
	@Override
	protected void initEvents() {
		mlistview.setOnRefreshListener(new OnRefreshListener2<ListView>() {

			@Override
			public void onPullUpToRefresh(
					PullToRefreshBase<ListView> refreshView) {
				
 			}

			@Override
			public void onPullDownToRefresh(
					PullToRefreshBase<ListView> refreshView) {
				adpter.clearData();
				uploadGroupMembers();
 			}
		});
		mlistview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
			     ((CustomApplication)getApplicationContext()).user = adpter.getItem(position-1);
				Bundle bundle = new Bundle();
				bundle.putInt("isother", 1);
				Intent intent = new Intent(GroupMembersActivity.this,UserInfosActivity.class);
				intent.putExtras(bundle);
				 startActivity(intent);			
			}
		});
	}
	
	private void uploadGroupMembers(){
		createWiatDialog("加载中");
		 ALCCPEngine.defauleEngine().getMemberOfGroup(group, new CallBack() {
			
			@Override
			public void done(Object object, AVException avexception) {
				cancleWaitDialog();
 				if(avexception == null){
 					List<AVUser> list = (List<AVUser>) object;
 					if(list == null || list.size() == 0){
 	 					showToast("没有成员");
 	 					return;
 					}
 					adpter.setData(list);
 				}else{
 					avexception.printStackTrace();
 					showToast("加载失败");
 				}
 				
 				adpter.notifyDataSetChanged();
			}
		});
	}
	

	class GroupAdpter extends BaseAdapter {
		private Context mcontext;
		private List<AVUser> datalist = null;
		private AQuery aq;

		@Override
		public int getCount() {
			if (datalist == null)
				return 0;
			return datalist.size();
		}

		public void setData(List<AVUser> datalist) {
			this.datalist = datalist;
		}

		public void clearData(){
			if(datalist != null)
				datalist.clear();
		}
		
		@Override
		public AVUser getItem(int position) {
			return datalist.get(position);
		}

		public GroupAdpter(Context mcontext) {
			this.mcontext = mcontext;
			aq = new AQuery(mcontext);
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view = LayoutInflater.from(mcontext).inflate(
					R.layout.adpter_groupmembers, null);
			final ImageView imv_heard = (ImageView) view
					.findViewById(R.id.imv_heard);
			TextView txv_groupname = (TextView) view
					.findViewById(R.id.txv_groupname);

			AVUser groupitem = datalist.get(position);
			String name = groupitem.getString("nickname");
			String picUrl = groupitem.getString("headViewURL");

			// default image
			final int width = DensityUtil.dip2px(mcontext, 70);
			Bitmap crclebitmap = BitmapUtils.drawCycleImage("#c4c4c5", width);
			Bitmap crclestrokebitmap = BitmapUtils.getCircleStrokbitmap(
					mcontext, crclebitmap, width, 1, "#eeeeee");
			imv_heard.setImageBitmap(crclestrokebitmap);
			

			aq.id(imv_heard).image(picUrl, true, true, width, 0,
					new BitmapAjaxCallback() {
						@Override
						protected void callback(String url, ImageView iv,
								Bitmap bm, AjaxStatus status) {
							super.callback(url, iv, bm, status);
							Bitmap mbitmap = BitmapUtils.getCircleStrokbitmap(
									mcontext, bm, width, 1, "#eeeeee");
							imv_heard.setImageBitmap(mbitmap);
						}
					});
			txv_groupname.setText(name);
			return view;
		}

	}



}
